package net.tngou.iask.dao;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import net.tngou.iask.jdbc.PageUtil;
import net.tngou.iask.jdbc.QueryHelper;
import net.tngou.iask.pojo.Main;
import net.tngou.iask.pojo.POJO;
import net.tngou.iask.util.IPv4Util;
import net.tngou.iask.util.StringUtil;

/**
 * 实体
 * @author tngou@tngou.net
 *
 */
public class MainDao extends BaseDao {
	
	/**
	 * 取得实体
	 * @param page 当前页
	 * @param size	页面大小
	 * @param province 省份
	 * @param keyword 关键词
	 * @return
	 */
	public PageUtil getPage(int page,int size,long province,String keyword) {
		List<Main> list=new ArrayList<>();
		int total=0;
		String sql_r="SELECT * ";
		String sql_t="SELECT COUNT(id) ";
		String sql_o=" ORDER BY id DESC";//排序
		String sql=" FROM `"+POJO.DABASE+"_main` ";
		if(StringUtil.isNotEmpty(keyword))
		{
			
			if(province!=0)
			{
				sql=sql+" WHERE province=? AND (name LIKE ? OR contacts LIKE ?) ";
				Object[] params={province,keyword+"%",keyword+"%"};
				list=QueryHelper.query_slice(Main.class,sql_r+sql+sql_o, page, size, params);			
				total=(int) QueryHelper.stat(sql_t+sql, params);
			}else
			{
				sql=sql+" WHERE name LIKE ? OR contacts LIKE ? ";
				Object[] params={keyword+"%",keyword+"%"};
				list=QueryHelper.query_slice(Main.class,sql_r+sql+sql_o, page, size, params);			
				total=(int) QueryHelper.stat(sql_t+sql, params);
			}
			
		}else{
			if(province!=0)
			{
				sql=sql+" WHERE province=?  ";
				list=QueryHelper.query_slice(Main.class,sql_r+sql+sql_o, page, size, province);
				total=(int) QueryHelper.stat(sql_t+sql, province);
			}else
			{
				list=QueryHelper.query_slice(Main.class,sql_r+sql+sql_o, page, size);
				total=(int) QueryHelper.stat(sql_t+sql);
			}
			
		}		
		return new PageUtil(list, page, size, total);
		
	}
	
	
}
